package com.nms.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.nms.handler.SystemInfoHandler;
import com.nms.model.Role;
import com.nms.model.User;
import com.nms.model.pojo.SystemInfo;
import com.nms.service.UserService;
import com.nms.util.UserLoginUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SystemInfo systemInfo;
	private UserService userService;
	private User user;
	private List<User> users;

	public UserAction() {
		systemInfo = new SystemInfo();
		SystemInfoHandler.getInstance().copySystemInfo(systemInfo);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public UserService getUserService() {
		return userService;
	}

	// @Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SystemInfo getSystemInfo() {
		return systemInfo;
	}

	public void setSystemInfo(SystemInfo systemInfo) {
		this.systemInfo = systemInfo;
	}

	// ///////////////////////////////////////////////////
	public String showIndexPage() {
		return "showIndexPage";
	}

	public String login() {
		if (user != null && (user = userService.login(user)) != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			/* 将用户添加到回话中 */
			session.put("user", user);
			/* 记录登录时间 */
			session.put("loginTime", new Date());
			System.out.println("user:" + user.getNickname() + " login! time:"
					+ session.get("loginTime"));
			return "loginSuccess";
		}
		return "loginFail";
	}

	public String adminLogin() {
		if (user != null && (user = userService.login(user)) != null) {
			if (user.getRole().getId() == 5)
				return "loginFail";
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			session.put("user", user);
			session.put("loginTime", new Date());
			System.out.println("admin:" + user.getNickname() + " login! time:"
					+ session.get("loginTime"));
			return "loginSuccess";
		}
		return "loginFail";
	}

	public String logout() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("user") != null) {
			session.put("user", null);
			session.put("loginTime", null);
			session.remove("user");
			session.remove("loginTime");
		}
		return "logout";
	}

	public String register() {
		Role role = new Role(5, "普通用户", "系统的一般用户");
		user.setRole(role);
		user.setGrade(0);
		user.setProperty(0);
		user.setHomepage("http://www.baidu.com");
		user.setRegisterTime(new Date());
		user.setState(1);
		if(user.getNickname() == null || user.getNickname().equals("")){
			user.setNickname(user.getUsername());
		}
		switch (userService.register(user)) {
		case 0:
			;
			break;
		case 1:
			return "registerSuccess";
		default:
			break;
		}
		return "registerFail";
	}

	public String applications() {
		return "applications";
	}

	public String download() {
		return "download";
	}

	public String showNews() {
		return "showNews";
	}

	// ////////////////////////////////////////////////
	public String editNews() {
		return "editNews";
	}

	public String newsManage() {
		return "newsManage";
	}

	public String userManage() {
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		users = userService.findAll();
		return "userManage";
	}

	public String srcManage() {
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		return "srcManage";
	}

	public String homepageManage() {
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		return "homepageManage";
	}

	public String manualEntry() {

		return "manualEntry";
	}

	public String autoPickNews() {

		return "autoPickNews";
	}

	public String publishNotice() {

		return "publishNotice";
	}

	public String setting() {

		return "setting";
	}

	public String staticalChart() {

		return "staticalChart";
	}

	public String staticalReport() {

		return "staticalReport";
	}
}
